import time

from appium import webdriver
import unittest
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.action_chains import ActionChains

"""
    1、如果 想要 通过 python 编写  app 的自动化测试脚本  ，需要使用  appium 客户端的服务 以及 appium 的模块
    2、 pypi：pip install Appium-Python-Client
"""


class MyAppiumTestCase(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        desired_caps = dict(
            platformName='Android',
            deviceName='127.0.0.1:62001',
            appActivity='com.tencent.mobileqq.activity.SplashActivity',
            appPackage='com.tencent.qqlite',
            noReset='true',
        )
        cls.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

        # 隐式等待
        cls.driver.implicitly_wait(30)

    @unittest.skip('old')
    def test_qq_old(self):
        print('qq启动成功-----')
        # 使用 TouchAction 来操作 滑动
        # 一个手指头

        time.sleep(5)

        touch_action = TouchAction(self.driver)

        touch_action. \
            press(x=213, y=595). \
            wait(0). \
            move_to(x=358, y=595). \
            wait(0). \
            move_to(x=506, y=595). \
            wait(0). \
            move_to(x=360, y=744). \
            wait(0). \
            move_to(x=213, y=888). \
            wait(0). \
            move_to(x=360, y=888). \
            wait(0). \
            move_to(x=506, y=888).release().perform()

        self.driver.save_screenshot('resources/qq.png')

    def test_qq_new(self):
        print('qq打开')
        # 使用 TouchAction 来操作 滑动
        # 获取 整个九宫格 的 大小

        lock = self.driver.find_element(AppiumBy.XPATH,
                                        "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.view.View[5]")

        lock_size = lock.size
        lock_position = lock.location
        print(lock_size)
        print(lock_position)
        # 步长
        step = lock_size['width'] / 6
        step_x = lock_size['width'] / 6
        step_y = lock_size['width'] / 6

        # 一个手指头

        time.sleep(5)

        touch_action = TouchAction(self.driver)
        point1 = (lock_position['x'] + step_x, lock_position['y'] + step_y)
        point2 = (point1[0] + step * 2, point1[1])
        point3 = (point2[0] + step * 2, point1[1])

        point4 = (point2[0], point3[1] + step * 2)

        point5 = (point1[0], point4[1] + step * 2)
        point6 = (point2[0], point5[1])
        point7 = (point3[0], point5[1])

        touch_action. \
            press(x=point1[0], y=point1[1]). \
            wait(0). \
            move_to(x=point2[0], y=point2[1]). \
            wait(0). \
            move_to(x=point3[0], y=point3[1]). \
            wait(0). \
            move_to(x=point4[0], y=point4[1]). \
            wait(0). \
            move_to(x=point5[0], y=point5[1]). \
            wait(0). \
            move_to(x=point6[0], y=point6[1]). \
            wait(0). \
            move_to(x=point7[0], y=point7[1]).release().perform()

        self.driver.save_screenshot('resources/qq.png')

    @classmethod
    def tearDownClass(cls) -> None:
        # cls.driver.quit()
        pass


if __name__ == '__main__':
    unittest.main()
